/*************************************************************************
	> File Name: observer.h
	> Author: ma6174
	> Mail: ma6174@163.com 
	> Created Time: 2021年11月29日 星期一 19时14分47秒
 ************************************************************************/

#include<iostream>
#include <string>
#include <unordered_set>

class Subject;

class Observer
{
public:
	Observer(std::string name, Subject* sub) : name(name), sub(sub) {}

	virtual void update() = 0;

protected:
	std::string name;
	Subject* sub;
};

//看股票的同事
class StockObserver : public Observer
{
public:
	using Observer::Observer;
	virtual void update();
};

//看NBA的同事
class NBAObserver : public Observer
{
public:
	using Observer::Observer;
	virtual void update();
};

class Subject
{
public:
	virtual void attach(Observer* o) = 0;
	virtual void detach(Observer* o) = 0;
	virtual void notify() = 0;
	virtual std::string getState() = 0;
	virtual void setState(std::string s) = 0;
};

class Boss : public Subject
{
public:
	virtual void attach(Observer* o)
	{
		observers.insert(o);
	}

	virtual void detach(Observer* o)
	{
		observers.erase(o);
	}

	virtual void notify()
	{
		for (auto o : observers)
			o->update();
	}

	virtual std::string getState()
	{
		return action;
	}

	virtual void setState(std::string s)
	{
		action = s;
	}

private:
	std::unordered_set<Observer*> observers;
	std::string action;
};


void StockObserver::update()
{
	std::cout << sub->getState() << "! " << name << ", " << "别看股票了，好好工作么! " << std::endl;
}

void NBAObserver::update()
{
	std::cout << sub->getState() << "! " << name << ", " << "别看ＮＢＡ了，好好工作呀! " << std::endl;
}
